"use strict";

const Controller = require("egg").Controller;
const md5 = require("md5");
const BaseController = require("./base");
class UserController extends BaseController {
    async jwtSign({ id, username }) {
        const { ctx, app } = this;

        const token = app.jwt.sign(
            {
                id,
                username,
            },
            app.config.jwt.secret
        );
        await app.redis.set(username, token, "EX", app.config.redisExpire);
        return token;
    }
    parseResulte(ctx, result) {
        return {
            ...ctx.helper.unPick(result.dataValues, ["password"]),
            createTime: ctx.helper.timestamp(result.createTime),
        };
    }
    async register() {
        const { ctx, app } = this;

        const params = ctx.params();
        const user = await ctx.service.user.getUser(params.username);
        if (user) {
            this.error("用户以存在");
            return;
        }
        const result = await ctx.service.user.add({
            ...params,
            password: md5(params.password + app.config.salt),
            createTime: ctx.helper.time(),
        });
        if (result) {
            const token = await this.jwtSign({ id: result.id, username: result.username });
            this.success({
                ...this.parseResulte(ctx, result),
                token,
            });
        } else {
            this.error(" 注册失败");
        }
    }
    async login() {
        const { ctx } = this;
        const { username, password } = ctx.params();
        const user = await ctx.service.user.getUser(username, password);
        if (user) {
            const token = await this.jwtSign({ id: user.id, username: user.username });
            this.success({
                ...this.parseResulte(ctx, user),
                token,
            });
        } else {
            this.error("该用户不存在");
        }
    }
    async detail() {
        const { ctx } = this;
        const user = await ctx.service.user.getUser(ctx.username);
        if (user) {
            this.success({
                ...this.parseResulte(ctx, user),
            });
        } else {
            this.error("该用户不存在");
        }
    }
    async logout() {
        const { ctx } = this;
        try {
            this.success("ok");
        } catch (err) {
            this.error("退出登入失败");
        }
    }

    async edit() {
        const { ctx } = this;
        const result = this.ctx.service.user.edit({
            ...this.ctx.params(),
            updateTime: ctx.helper.time(),
        });
        this.success(result);
    }
}

module.exports = UserController;
